﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;

namespace WpfWordsLearner
{
	public class OnDragEnterTigger : TriggerAction<TreeView>
	{
		protected override void OnAttached()
		{
			base.OnAttached();
		}

		protected override void OnDetaching()
		{
			base.OnDetaching();
		}

		protected override void Invoke(object parameter)
		{
			if (null != parameter && (parameter as DragEventArgs).Data != null
				&& ((parameter as DragEventArgs).Data as DataObject).ContainsFileDropList())
			{
				//RunStoryboard("Timeline1");
				(parameter as DragEventArgs).Handled = true;
				(parameter as DragEventArgs).Effects = DragDropEffects.Copy;
			}
			else if (null != parameter && (parameter as DragEventArgs).Data.GetDataPresent(typeof(Model.WordsModel)))
			{
				var TVI = ((parameter as DragEventArgs).OriginalSource as DependencyObject).FindAnchestor<TreeViewItem>();

				if (TVI != null && TVI.Header is Model.DirectoryModel)
				{
					(parameter as DragEventArgs).Handled = true;
					if ((parameter as DragEventArgs).OriginalSource is Path && !TVI.IsExpanded)
						TVI.ExpandSubtree();

					(parameter as DragEventArgs).Effects = DragDropEffects.None;
				}
			}
		}

		void RunStoryboard(string name)
		{
			Storyboard sb = AssociatedObject.FindResource(name) as Storyboard;
			System.Diagnostics.Debug.Assert(sb != null);
			sb.Begin();
		}
	}
}